$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/transaction/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
            { label: '交易时间', name: 'createtime', index: 'createtime', width: 80 },
			{ label: '业务单号', name: 'businessnum', index: 'businessNum', width: 80 },
			{ label: '交易类型', name: 'type', index: 'type', width: 80,formatter: function(item, index) {
                    if (item === 0) {
                        return '货架收入';
                    }
                    if (item === 1) {
                        return '外卖收入';
                    }
                    if (item === 2) {
                        return '提现';
                    }
                    if (item === 3) {
                        return '提现失败退回';
                    }
                }  },
			{ label: '支付流水号', name: 'serialnum', index: 'serialNum', width: 80 }, 			
			{ label: '支付模式', name: 'paymode', index: 'paymode', width: 80,formatter: function(item, index) {
                    if (item === 0) {
                        return '微信支付-自有';
                    }
                    if (item === 1) {
                        return '微信支付-代收';
                    }

                } },
			{ label: '交易金额', name: 'cost', index: 'cost', width: 80 }

        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			number:null,
			type:null,
            paymode:null,
            startime:null,
            endtime:null
		},
		showList: true,
		title: null,
		transaction: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.transaction = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.transaction.id == null ? "sys/transaction/save" : "sys/transaction/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.transaction),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/transaction/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/transaction/info/"+id, function(r){
                vm.transaction = r.transaction;
            });
		},
		reload: function (event) {
            var startime = $("#startime").val();
            var endtime = $("#endtime").val();
            if((startime!=''&&typeof startime!='undefined')&&(endtime!=''&&typeof endtime!='undefined')){
                var st = new Date(startime).getTime();
                var et = new Date(endtime).getTime();
                if(st>et){
                    alert("开始时间不能大于结束时间");
                    return;
                }
            }
			vm.showList = true;
            if(endtime!=''&&typeof endtime!='undefined'){
                var date = new Date(new Date(endtime).getTime()+(24*60*60*1000));
                var premonth   = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
                if(premonth<10){
                    premonth = "0"+premonth;
                }
                var preday = date.getDate(); //获取当前日(1-31)
                if(preday<10){
                    preday = "0"+preday;
                }
                endtime = date.getFullYear()+"-"+premonth+"-"+preday;
            }
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'number':vm.q.number,'statu':vm.q.statu,'type':vm.q.type,'startime':startime,'endtime':endtime},
                page:page
            }).trigger("reloadGrid");
		}
	}
});